package com.fafu.control;

import com.fafu.model.Manager;
import com.fafu.pojo.ManagerListPojo;
import com.fafu.pojo.StudentSearchRepair;
import com.fafu.service.ManagerOperationService;
import com.fafu.service.StudentRepairService;
import com.fafu.service.UserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import util.Dictionary;
import util.JsonRsp;
import util.MyMap;

import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static util.Dictionary.CODE.PARAM_ERROR;
import static util.Dictionary.CODE.SUCCESS;
import static util.Dictionary.CODE.SYSTEM_ERROR;
import static util.Dictionary.REPAIR.*;
import static util.Dictionary.REPAIR.REPAIR_STATE_REFUSE;

@RestController()
@RequestMapping("/manager")
@Api("管理员相关操作")
public class ManagerOperationControl {
    private Logger logger = LoggerFactory.getLogger(ManagerOperationControl.class);

    @Autowired
    private UserService userService;

    @Autowired
    private StudentRepairService studentRepairService;

    @Autowired
    private ManagerOperationService managerOperationService;
    @ApiOperation("添加管理员")
    @ResponseBody
    @RequestMapping(value = "/addManager",method = RequestMethod.POST)
    public JsonRsp addManager(
            HttpServletRequest request
            ,@RequestBody @ApiParam("管理员信息/记得上传添加管理员")Manager manager){
        try{
            Map<String,Object> reMap = userService.addUser(request, new MyMap().put("manager",manager).toMap());
            Integer code = (Integer) reMap.get("code");
            if(code.equals(SUCCESS))
                return JsonRsp.success("");
            return JsonRsp.fail(code, (String) reMap.get("msg"));
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(SYSTEM_ERROR,"系统内部错误");
        }
    }
    @ApiOperation("修改管理员使用权")
    @ResponseBody
    @RequestMapping(value = "/changeManagerUsed",method = RequestMethod.GET)
    public JsonRsp addManager(
            @RequestParam(value = "opid")@ApiParam("管理员Id") Integer opid
            ,@RequestParam(value="chid")@ApiParam("被修改管理员的id")Integer chid){
        try{
            Map<String,Object> reMap = managerOperationService.changeUsed(new MyMap()
                    .put("opid",opid).put("chid",chid).toMap());
            Integer code = (Integer) reMap.get("code");
            if(code == SUCCESS)
                return JsonRsp.success("success");
            else
                return JsonRsp.fail((Integer) reMap.get("code"), (String) reMap.get("msg"));
        }catch(Exception e){
            logger.error(""+e);
            return JsonRsp.fail(SYSTEM_ERROR,"system error");
        }
    }
    @ApiOperation("删除用户")
    @ResponseBody
    @RequestMapping(value = "/deleteManager",method=RequestMethod.POST)
    public JsonRsp deleteManager(
            @RequestParam(value = "id")@ApiParam("用户id") Integer id){
        try{
            userService.deleteUser(new MyMap().put("id",id).toMap());
            return JsonRsp.success("");
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(SYSTEM_ERROR,"系统内部错误");
        }
    }

    @ApiOperation("获得ManagerList")
    @ResponseBody
    @RequestMapping(value = "/getManagerList",method=RequestMethod.POST)
    public JsonRsp getManagerList(
            @ApiParam("第几页")@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum
            ,@ApiParam("每页几条，默认20")@RequestParam(value = "pageSize",defaultValue = "20")Integer pageSize){
        try{
            Map<String,Object> reMap = userService.searchManagerList(new MyMap().put("pageNum",pageNum).put("pageSize",pageSize).toMap());

            return JsonRsp.success(reMap.get("data")).setCount((Long) reMap.get("count"));
        }catch (Exception e){
            logger.error(e.getMessage());
            return JsonRsp.fail(SYSTEM_ERROR,"系统内部错误");
        }
    }

    @ApiOperation("拒绝报修")
    @ResponseBody
    @RequestMapping(value = "/refuseRepair",method=RequestMethod.POST)
    public JsonRsp refuseRepair(
            @ApiParam(value = "id")@RequestParam(value = "id")Integer id
            ,@ApiParam(value="操作员id")@RequestParam(value = "operationId") Integer operationManager
            ,@ApiParam(value = "msg")@RequestParam(value = "msg")String msg){
        try{
            Map<String,Object> resultMap = managerOperationService.refuse(new MyMap()
                    .put("id",id)
                    .put("operationId",operationManager)
                    .put("msg",msg).toMap());
            Integer code = (Integer) resultMap.get("code");
            if(code.equals(SUCCESS))
                return JsonRsp.success(null);
            return JsonRsp.fail(code, (String) resultMap.get("msg"));
        }catch(Exception e){
            logger.error(""+e);
            return JsonRsp.fail(SYSTEM_ERROR,"system error");
        }

    }
    @ApiOperation("管理员提交报修")
    @ResponseBody
    @RequestMapping(value = "/managerRepair",method=RequestMethod.POST)
    public JsonRsp managerRepair(
            @ApiParam(value = "id")@RequestParam(value = "id")Integer id
            ,@ApiParam(value="操作员id")@RequestParam(value = "operationId") Integer operationManager
            ,@ApiParam(value="师傅id")@RequestParam(value = "workerId") Integer workerId){


        try{
            Map<String,Object> resultMap = managerOperationService.repair(new MyMap()
                    .put("id",id)
                    .put("operationId",operationManager)
                    .put("workerId",workerId)
                    .toMap());
            Integer code = (Integer) resultMap.get("code");
            if(code.equals(SUCCESS))
                return JsonRsp.success(null);
            return JsonRsp.fail(code, (String) resultMap.get("msg"));
        }catch(Exception e){
            logger.error(""+e);
            return JsonRsp.fail(SYSTEM_ERROR,"system error");
        }
    }

    @ApiOperation("查询学生历史。进行.list")
    @ResponseBody
    @RequestMapping(value = "/searchList",method=RequestMethod.POST)
    public JsonRsp managerSearchHistoryList(
            @ApiParam(value="操作员id")@RequestParam(value = "operationId") Integer operationId
            ,@ApiParam(value="type:历史0 进行中:3 拒绝：4")@RequestParam(value = "type",required = false) Integer type
            ,@ApiParam(value="size:全部：0 自己：1")@RequestParam(value = "size") Integer size
            ,@ApiParam(value="pageNum")@RequestParam(value = "pageNum") Integer pageNum
            ,@ApiParam(value="pageSize")@RequestParam(value = "pageSize") Integer pageSize){
        try{
//            if(type == null)
//                return JsonRsp.fail(Dictionary.CODE.PARAM_IS_LOST,"type为空");
            MyMap paramMap = new MyMap();
            paramMap.put("type",type).put("operationId",operationId).put("size",size);
            if(operationId != null)
                paramMap.put("operationId",operationId);
            Map<String,Object> reMap = managerOperationService.managerSearchRepair(paramMap.put("pageNum",pageNum).put("pageSize",pageSize).toMap());

            return JsonRsp.success(reMap.get("data")).setCount((Long) reMap.get("count"));

        }catch(Exception e){
            logger.error(""+e);
            return JsonRsp.fail(SYSTEM_ERROR,"system error");
        }
    }

    @ApiOperation("查询公共历史。进行.list")
    @ResponseBody
    @RequestMapping(value = "/searchOtherList",method=RequestMethod.POST)
    public JsonRsp searchOtherList(
            @ApiParam(value="操作员id")@RequestParam(value = "operationId") Integer operationId
            ,@ApiParam(value="type:历史0 进行中:3 拒绝：4")@RequestParam(value = "type",required = false) Integer type
            ,@ApiParam(value="size:全部：0 自己：1")@RequestParam(value = "size") Integer size
            ,@ApiParam("第几页")@RequestParam(value = "pageNum", defaultValue = "1")Integer pageNum
            ,@ApiParam("每页几条，默认20")@RequestParam(value = "pageSize",defaultValue = "20")Integer pageSize){
        try{
            MyMap paramMap = new MyMap();
            paramMap.put("type",type).put("operationId",operationId).put("size",size);
            if(operationId != null)
                paramMap.put("operationId",operationId);
            Map<String,Object> reMap = managerOperationService.managerSearchOtherRepair(paramMap.put("pageNum", pageNum).put("pageSize",pageSize).toMap());
            return JsonRsp.success(reMap.get("data")).setCount((Long) reMap.get("count"));

        }catch(Exception e){
            logger.error(""+e);
            return JsonRsp.fail(SYSTEM_ERROR,"system error");
        }
    }
}
